<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Fblogin_model extends CI_Model {
    
    public function __construct()
    {
        parent::__construct();
		
    }
	public function login_by_fb($fb_data)
	{
		if(is_array($fb_data['me']))
		{
			$query = $this->db->query("SELECT * FROM user WHERE email = '".$fb_data['me']['email']."' AND email_confirmed = '1'");
       		if($query->num_rows()>0)
			{
				foreach($query->result() as $rows)
				{
					$newdata = array('user_id'=> $rows->id,'user_name'=>$rows->name,'user_email'=> $rows->email,'country_id'=>$rows->country, 'logged_in' => TRUE);
				}
				$this->session->set_userdata($newdata);
			}
			else
			{
				$username	=	$fb_data['me']['first_name'].' '.$fb_data['me']['last_name'];
				$dob_arr	=	explode('/',$newdata['user_id']['birthday']);
				$dob	=	$dob_arr[2].'-'.$dob_arr[0].'-'.$dob_arr[1];
				if($fb_data['me']['gender']=='male'){$gen	=	'M';}else{$gen	=	'F';}
				$this->db->query("INSERT INTO `user` SET `name` = '".$fb_data['me']['first_name']."', `fullname` = '".$username."', `email` = '".$fb_data['me']['email']."',`country` = '4', `dob` = '".$dob."', `gender` = '".$gen."',`email_confirmed` = '1'");
				$uid	=	$this->db->insert_id();
				
				$newdata = array('user_id'=> $uid,'user_name'=>$username,'user_email'=>$fb_data['me']['email'],'logged_in'=>TRUE);
				$this->session->set_userdata($newdata);
				
			}
			
			$number=$this->db->query("SELECT * FROM ci_sessions WHERE `user_data` = '".$newdata['user_id']."' ");
			$no=$number->num_rows();
			if($no>0){
					$this->db->query("UPDATE `ci_sessions` SET 
					`session_id` = '".$this->session->userdata['session_id']."', 
					`ip_address` = '".$this->session->userdata['ip_address']."', 
					`user_agent` = '".$this->session->userdata['user_agent']."', 
					`last_activity` = '".$this->session->userdata['last_activity']."'
					WHERE `user_data` = '".$newdata['user_id']."'
					");
						
			}
			else{ 
				$this->db->query("INSERT INTO `ci_sessions` SET `session_id` = '".$this->session->userdata['session_id']."', `ip_address` = '".$this->session->userdata['ip_address']."', `user_agent` = '".$this->session->userdata['user_agent']."', `user_data` = '".$newdata['user_id']."', `last_activity` = '".$this->session->userdata['last_activity']."'");
			}
			return true;
		}
		else
		{
			return false;
		}
	}
	public function login_with_fb($fql_query_obj)
	{
		/*echo 'started....---><pre>';;
		print_r($fql_query_obj);*/
		$this->load->library('Getimage');
		if(is_array($fql_query_obj))
		{	
			$query = $this->db->query("SELECT * FROM user WHERE email = '".$fql_query_obj['data'][0]['email']."' AND email_confirmed = '1'");
       		if($query->num_rows()>0)
			{
				foreach($query->result() as $rows)
				{
					$newdata	=	array('user_id'=> $rows->id,'user_name'=>$rows->name,'user_email'=> $rows->email,'logged_in' => TRUE);
					$uid	=	$rows->id;
				}
				$this->session->set_userdata($newdata);
				$this->getimage->source		=	$fql_query_obj['data'][0]['pic_big'];
				$this->getimage->save_to	=	'./images/profile_tmp/'; // with trailing slash at the end
				$get	=	$this->getimage->download('curl'); // using GD
				/*echo 'entered--  1  ---->';
				print_r($this->getimage);
				die('pkm end');*/
			}
			else
			{
				$username	=	$fql_query_obj['data'][0]['name'];
				$dob_arr	=	explode('/',$fql_query_obj['data'][0]['birthday_date']);
				$dob	=	$dob_arr[2].'-'.$dob_arr[0].'-'.$dob_arr[1];
				if($fql_query_obj['data'][0]['sex']=='male'){$gen	=	'M';}else{$gen	=	'F';}
				$this->db->query("INSERT INTO `user` SET `name` = '".$fql_query_obj['data'][0]['first_name']."', `fullname` = '".$username."', `email` = '".$fql_query_obj['data'][0]['email']."', `country`='4', `dob` = '".$dob."', `gender` = '".$gen."',`email_confirmed` = '1'");
				$uid	=	$this->db->insert_id();
				
				$newdata = array('user_id'=> $uid,'user_name'=>$username,'user_email'=>$fql_query_obj['data'][0]['email'],'logged_in'=>TRUE);
				$this->session->set_userdata($newdata);
				
				//copy image from face book
				$this->getimage->source		=	$fql_query_obj['data'][0]['pic_big'];
				$this->getimage->save_to	=	'./images/profile_tmp/'; // with trailing slash at the end
				$get	=	$this->getimage->download('curl'); // using GD
				/*echo 'entered--- 2 ---->';
				print_r($this->getimage);
				die('pkm end');*/
			}
			if($get)
			{
				$this->load->library('uploadfile');
				$handle_profile = new uploadfile($this->getimage->save_to.$this->getimage->image_name);
				$handle_thumb = new uploadfile($this->getimage->save_to.$this->getimage->image_name);
				$handle_chat = new uploadfile($this->getimage->save_to.$this->getimage->image_name);
				if ($handle_profile->uploaded && $handle_thumb->uploaded && $handle_chat->uploaded) 
				{
					$handle_profile->file_safe_name = true;
					$handle_profile->image_resize   = true;
					$handle_profile->image_x        = 178;
					$handle_profile->image_y  		= 203;					
					$handle_profile->process('./images/profile/');
					
					$handle_thumb->file_safe_name 		= true;
					$handle_thumb->file_name_body_pre 	= 'thumb_';
					$handle_thumb->image_resize   		= true;
					$handle_thumb->image_x        		= 80;
					$handle_thumb->image_y        		= 100;					
					$handle_thumb->process('./images/profile/');
					
					$handle_chat->file_safe_name 		= true;
					$handle_chat->file_name_body_pre 	= 'chat_';
					$handle_chat->image_resize   		= true;
					$handle_chat->image_x       		= 30;
					$handle_chat->image_y  				= 30;					
					$handle_chat->process('./images/profile/');
					
					if ($handle_profile->processed) {
						$response['file_src_name'] = $handle_profile->file_dst_name;
						$response['file_thumb_name'] = $handle_thumb->file_dst_name;
						$response['file_chat_name'] = $handle_chat->file_dst_name;
						$handle_profile->clean();
						$handle_thumb->clean();
						$handle_chat->clean();
					} else {
						$response['message'] = $handle_profile->error;
					}
				}

			}
			$prof_img=$this->db->query("SELECT * FROM profile_info WHERE `user_id` = '".$uid."'");
			$no_prof_img=$prof_img->num_rows();
			if($no_prof_img>0)
			{
				$this->db->query("UPDATE `profile_info` SET `prof_image_original` = '".$response['file_src_name']."', `prof_image_thumb` = '".$response['file_thumb_name']."', `prof_image_chat` =	'".$response['file_chat_name']."' WHERE `user_id` = '".$uid."'");
			}
			else
			{
				$this->db->query("INSERT INTO `profile_info` SET `prof_image_original` = '".$response['file_src_name']."', `prof_image_thumb` = '".$response['file_thumb_name']."', `prof_image_chat` =	'".$response['file_chat_name']."', `user_id` = '".$uid."'");
			}
			$number=$this->db->query("SELECT * FROM ci_sessions WHERE `user_data` = '".$uid."' ");
			$no=$number->num_rows();
			if($no>0)
			{
					$this->db->query("UPDATE `ci_sessions` SET 
					`session_id` = '".$this->session->userdata['session_id']."', 
					`ip_address` = '".$this->session->userdata['ip_address']."', 
					`user_agent` = '".$this->session->userdata['user_agent']."', 
					`last_activity` = '".$this->session->userdata['last_activity']."'
					WHERE `user_data` = '".$uid."'
					");
						
			}
			else
			{ 
				$this->db->query("INSERT INTO `ci_sessions` SET `session_id` = '".$this->session->userdata['session_id']."', `ip_address` = '".$this->session->userdata['ip_address']."', `user_agent` = '".$this->session->userdata['user_agent']."', `user_data` = '".$uid."', `last_activity` = '".$this->session->userdata['last_activity']."'");
			}
			
			return true;
		}
		else
		{
			return false;
		}
		
	}
}